#!/usr/bin/env bash
#获取美丽说app的pid
if [ $# != 2 ]; then
	echo "sh get-android-pid.sh 4df742f8263bcf3d meilishuo"
	exit 1;
fi
deviceId=$1
appName=$2

if [ "$appName" = "" ];then
    appName="meilishuo"
fi

currentDir=$(cd "$(dirname "$0")"; pwd)
echo "current dir : "$currentDir
datDir=$currentDir/../../../target/android-info/battery/dat
htmlDir=$currentDir/../../../target/android-info/battery/html

if [ ! -d "$datDir" ]; then
    mkdir -p $datDir
fi
if [ ! -d "$htmlDir" ]; then
    mkdir -p $htmlDir
fi
touch ${currentDir}/../../../target/android-info/battery/${deviceId}.device

pidFile=${datDir}/${appName}.pid
processFile=${datDir}/process.pid

if [ -f "$pidFile" ]; then
    echo "exist, rm"
    rm $pidFile
fi

sh ${currentDir}/get-android-cpufreq.sh $deviceId

echo "start--"$pidFile
adb -s $deviceId shell top -n 1 | grep $appName  | awk '{print $1}' > ${pidFile}
adb -s $deviceId shell top -n 1 | grep $appName | awk '{print $1,$10}' > ${processFile}
echo "end"
while read LINE
do
     user=`echo $LINE`
     echo "start fetch cpustat info, "$deviceId", "$user
     sh $currentDir/get-android-cpustat.sh $deviceId $user &

done < ${pidFile}

echo "start fetch cpu info, "$deviceId
sh $currentDir/get-android-cpu.sh $deviceId $appName &

echo "start fetch mem info, "$deviceId", "$appName
sh $currentDir/get-android-mem.sh $deviceId $appName &

echo "start fetch net infi, "$deviceId", "$user
sh $currentDir/get-android-net.sh $deviceId $user &

echo "starting recording..."
